<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Coalesce 函数示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7f6;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            text-align: center;
        }

        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 600px;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        .input-group {
            margin: 20px 0;
        }

        .input-group label {
            font-size: 18px;
            margin-right: 10px;
        }

        .input-group input {
            padding: 8px;
            font-size: 16px;
            width: 60%;
            margin-right: 10px;
            border-radius: 4px;
            border: 1px solid #ccc;
        }

        .btn {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 30px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .result span {
            color: #4CAF50;
        }

        .code-box {
            background-color: #f0f0f0;
            padding: 15px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            word-wrap: break-word;
            white-space: pre-wrap;
            text-align: left;
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Coalesce 函数示例</h1>
        <p>输入多个值，点击按钮来获取第一个非 `undefined` 或 `null` 的值。</p>

        <div class="input-group">
            <label for="value1">值1:</label>
            <input type="text" id="value1" placeholder="请输入值1" />
        </div>

        <div class="input-group">
            <label for="value2">值2:</label>
            <input type="text" id="value2" placeholder="请输入值2" />
        </div>

        <div class="input-group">
            <label for="value3">值3:</label>
            <input type="text" id="value3" placeholder="请输入值3" />
        </div>

        <button class="btn" onclick="findCoalescedValue()">获取非空值</button>

        <div class="result">
            <p>第一个非 `undefined` 或 `null` 的值是:</p>
            <div class="code-box" id="result"></div>
        </div>
    </div>

    <script>
        // Coalesce 函数实现：返回第一个非 undefined 或 null 的值
        const coalesce = (...args) => args.find(v => ![undefined, null].includes(v));

        // 获取并展示第一个非 null 或 undefined 的值
        function findCoalescedValue() {
            const value1 = document.getElementById("value1").value || null;
            const value2 = document.getElementById("value2").value || null;
            const value3 = document.getElementById("value3").value || null;

            // 调用 coalesce 函数
            const result = coalesce(value1, value2, value3);

            // 显示结果
            document.getElementById("result").textContent = result !== undefined ? result : "没有找到非 null 或 undefined 的值";
        }
    </script>
</body>

</html>